# 更新我买网物流信息

import os
import sys
import django
import datetime

# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# sys.path.extend([BASE_DIR, ])
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
# django.setup()

base_url = os.path.split(os.path.split(os.getcwd())[0])[0]
print(base_url)
sys.path.insert(0, os.path.split(os.path.split(os.getcwd())[0])[0])
os.environ['DJANGO_SETTINGS_MODULE'] = 'finance_c2c.settings'
django.setup()

from core.tools.get_datetime import GetDateTime
from apps.supply_order.models import PostRecord
from apps.supply_order.models import WomaiOrder
from apps.supply_order.models import Logistics
from apps.supply_order.womai_api import WoMaiApi
from apps.supply_order.synquery import kuai_di_result


class WMLogistics(WoMaiApi):
    def get_order(self):
        return WomaiOrder.objects.filter(order_status__in=[2, 3])

    def logistics(self):
        queryset = self.get_order()
        for item in queryset:
            if item.company.sign == "womaiwang":
                pass
            else:
                try:
                    logistics_obj = Logistics.objects.get(
                        order=item
                    )
                    if not logistics_obj.ischeck:
                        content = kuai_di_result(logistics_obj.post_company.code, logistics_obj.code)
                        if content.get("status") and content.get("status") == "200":
                            data = content.get("data")
                            for i in data:
                                PostRecord.objects.get_or_create(
                                    logistics=logistics_obj,
                                    remark=i.get("context"),
                                    create_time=i.get("ftime"),
                                    other_state_code=i.get("state"),
                                    statename=i.get("context"),
                                )
                            logistics_obj.ischeck = content.get("ischeck")
                            logistics_obj.update_time = datetime.datetime.now()
                            logistics_obj.save()
                except Exception as e:
                    print(e)
                    # logistics_data = self.get_logistics(i.tid)
                    # print(logistics_data)
                    # if logistics_data.get("logistics"):
                    #     logistics_list = logistics_data.get("logistics")
                    #     for l in logistics_list:
                    #         create_time = datetime.datetime.strptime(l.get("createtime"), "%Y/%m/%d %H:%M:%S")
                    #         Logistics.objects.get_or_create(
                    #             order=i,
                    #             statecode=l.get("statecode"),
                    #             statename=l.get("statename"),
                    #             remark=l.get("remark"),
                    #             stationname=l.get("stationname"),
                    #             create_time=create_time
                    #         )
                    #         i.order_status = 3
                    #         i.save()
                    #


if __name__ == '__main__':
    WMLogistics = WMLogistics()
    WMLogistics.logistics()
